﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Img;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Img
{
    /// <summary>
    /// 根据生产批次获取图片信息请求命令处理类
    /// </summary>
    public class GetPictureHandler:IRequestHandler<GetPictureCommand,ApiResult<List<PictureDTO>>>
    {
        IBaseRepository<Picture_Information> pictureRep;
        IMapper mapper;

        public GetPictureHandler(IBaseRepository<Picture_Information> pictureRep, IMapper mapper)
        {
            this.pictureRep = pictureRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<PictureDTO>>> Handle(GetPictureCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<PictureDTO>> res = new ApiResult<List<PictureDTO>>();

                var pictures = pictureRep.GetAll().Where(x => !x.IsDeleted&&x.Production_Batch==request.production_Batch&&x.PictureType==request.pictureType);
                var model=mapper.Map<List<PictureDTO>>(pictures);
                res.Msg = "根据生产批次获取图片信息";
                res.Code = ApiEnum.Success;
                res.Data = model;
                return Task.FromResult(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
